﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using FileSquirrel.Logic.XSD;

namespace FileSquirrel.Logic
{
    public static class Reader
    {
        private static LibraryDataSet _globalLibrary;
        private static UserSettings _userSettings;
        public static string _libName;
        public static string _libdir;

        #region Property Procedures

        public static LibraryDataSet GlobalLibrary
        {
            get
            {
                if (_globalLibrary == null || _globalLibrary._Files.Count() == 0)
                {
                    _globalLibrary = GetFilesDataSet();
                }

                return _globalLibrary;
            }
            set { _globalLibrary = value; }
        }

        public static LibraryDataSet GlobalRequestLibrary { get; set; }

        public static UserSettings UserSettings
        {
            get { return _userSettings ?? (_userSettings = GetSettings()); }
            set { _userSettings = value; }
        }

        #endregion Property Procedures

        public static void ResetToOwnLibrary()
        {
            GlobalLibrary = GetFilesDataSet();
        }

        private static LibraryDataSet GetFilesDataSet()
        {

            LibraryDataSet dsFiles = new LibraryDataSet();
            string libraryPath = Path.Combine(_libdir, string.Format("{0}.{1}", _libName, "fsl"));

            if (File.Exists(libraryPath))
                dsFiles.ReadXml(libraryPath);

            dsFiles._Files.DurationColumn.DefaultValue = new TimeSpan();

            return dsFiles;
        }

        public static LibraryDataSet.FilesDataTable GetDistinctFilesDataSet(LibraryDataSet.FilesDataTable compareLib, string filename)
        {
            LibraryDataSet newFilesLib = new LibraryDataSet();
            newFilesLib.ReadXml(filename);

            List<LibraryDataSet.FilesRow> delRows = new List<LibraryDataSet.FilesRow>();

            var seriesonlyrows = from c in newFilesLib._Files.AsEnumerable()
                                 where c.Category.Equals("SERIES", System.StringComparison.CurrentCultureIgnoreCase)
                                 select c;

            var otherrows = newFilesLib._Files.AsEnumerable().Select(e => e).Except(seriesonlyrows);

            foreach (LibraryDataSet.FilesRow row in seriesonlyrows)
            {
                if (!row.IsShowNull() && !row.IsSeasonNull() && !row.IsEpisodeNrNull())
                {
                    if (compareLib.Select(string.Format("Show = '{0}' AND Season = '{1}' AND EpisodeNr = '{2}'", row.Show.Replace(@"'", "''"), row.Season.Replace(@"'", "''"), row.EpisodeNr.Replace(@"'", "''"))).Length > 0)
                        delRows.Add(row);
                }
            }

            foreach (LibraryDataSet.FilesRow row in otherrows)
            {
                if (compareLib.Select("Name = '" + row.Name.Replace(@"'", "''") + "'").Length > 0)
                    delRows.Add(row);
            }

            foreach (LibraryDataSet.FilesRow row in delRows)
            {
                newFilesLib._Files.RemoveFilesRow(row);
            }

            _globalLibrary = newFilesLib;

            return newFilesLib._Files;
        }

        private static UserSettings GetSettings()
        {
            UserSettings dsSettings = new UserSettings();

            dsSettings.ReadXml(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "XML\\UserSettings.xml"));
            //dsSettings.ReadXml(Path.GetFullPath("XML/UserSettings.xml"));

            return dsSettings;
        }
    }
}